home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / util / cli / LibMon.lha / LibMon.c < prev    next >
C/C++ Source or Header  |  1996-12-04  |  10KB  |  489 lines

  1. /*
  2.  * Library Monitor v1.25
  3.  * © Dec 4 1996 Karl J. Ots
  4.  */
  5.  
  6. /* Include files */
  7. #include    <exec/types.h>
  8. #include    <clib/exec_protos.h>
  9. #include    <clib/dos_protos.h>
  10. #include    <exec/libraries.h>
  11. #include    <exec/execbase.h>
  12. #include    <dos/dos.h>
  13. #include    <dos/rdargs.h>
  14.  
  15. #include    <stdio.h>
  16. #include    <stdlib.h>
  17. #include    <string.h>
  18.  
  19.  
  20. /* Functions and Procedures */
  21. void LoadLib(STRPTR LibName, BOOL Report);
  22. void LoadLibs(void);
  23. void FlushLib(STRPTR LibName);
  24. void FlushLibs(void);
  25. void ListLib(STRPTR LibName);
  26. void ListLibs(void);
  27.  
  28. struct LibInfo *BuildLibInfoList(struct List *LibList);
  29.  
  30. struct LibInfo *AddLibInfo(struct LibInfo *LibInfo, struct Library *Library);
  31. void KillLibInfo(struct LibInfo *LibInfo);
  32. struct LibInfo *GetLibInfoHead(struct LibInfo *LibInfo);
  33. struct LibInfo *GetLibInfoTail(struct LibInfo *LibInfo);
  34.  
  35. void OutputHeader(void);
  36. void OutputUsage(void);
  37. void OutputLibInfoHeader(void);
  38. void OutputLibInfo(struct LibInfo *li);
  39. void OutputFlushedLibInfo(struct LibInfo *li);
  40. void OutputLibNotFound(STRPTR name);
  41.  
  42. void PlainText(void);
  43. void BoldText(void);
  44. void Colour2Text(void);
  45. void Colour3Text(void);
  46.  
  47.  
  48. /* Macros and constants */
  49. #define CTRL_C        (SetSignal(NULL,NULL) & SIGBREAKF_CTRL_C)
  50. #define BREAK_TXT    "***Break - LibMon"
  51.  
  52. #define REPORT        TRUE
  53. #define QUIET        FALSE
  54.  
  55. #define PROG_DATE        "Dec 4 1996"
  56. #define PROG_VERS        "1.25"
  57.  
  58.  
  59. /* Structures and related constants */
  60. #define LA_TEMPLATE    "LIBS=LIBNAMES/M,A=ALL/S,L=LIST/S,F=FLUSH/S"
  61.  
  62. struct LibArgs {
  63.     STRPTR    *LIBNAMES;
  64.     ULONG    ALL;
  65.     ULONG    LIST;
  66.     ULONG    FLUSH;
  67.     ULONG    Flags;
  68. };
  69.  
  70. /* LibArgs.Flags bit numbers */
  71. #define LAFB_LIBNAMES    31
  72. #define LAFB_ALL    0
  73. #define LAFB_LIST    1
  74. #define    LAFB_FLUSH    2
  75.  
  76. /* LibArgs.Flags values */
  77. #define LAFF_NONE    0
  78. #define LAFF_LIBNAMES    (1<<LAFB_LIBNAMES)
  79. #define LAFF_ALL    (1<<LAFB_ALL)
  80. #define LAFF_LIST    (1<<LAFB_LIST)
  81. #define LAFF_FLUSH    (1<<LAFB_FLUSH)
  82.  
  83. struct LibInfo {
  84.     struct LibInfo *Next;
  85.     struct LibInfo *Prev;
  86.     char    Name[80];
  87.     ULONG    Address;
  88.     UWORD    Version;
  89.     UWORD    Revision;
  90.     BYTE    Priority;
  91.     UWORD    OpenCount;
  92.     BOOL    Flushed;
  93. };
  94.  
  95.  
  96. /* Information for C:Version */
  97. const char verstr[] = "$VER: LibMon "PROG_VERS" ("PROG_DATE")";
  98.  
  99.  
  100. /* Global Variables */
  101. extern struct ExecBase *SysBase;
  102.  
  103.  
  104. /* Start */
  105. void main(void)
  106. {
  107.     struct RDArgs *rdargs = NULL;
  108.     struct LibArgs LibArgs = {NULL,FALSE,FALSE,FALSE,LAFF_NONE};
  109.     int i = 0;
  110.     
  111.     OutputHeader();
  112.     
  113.     /* Check OS version */
  114.     if (SysBase->LibNode.lib_Version < 36)
  115.     {
  116.         puts("Sorry, but this program requires OS2.0 or above");
  117.         exit(0);
  118.     }
  119.  
  120.     /* Collect arguments */
  121.     rdargs = ReadArgs(LA_TEMPLATE,&LibArgs,NULL);
  122.     if (rdargs == NULL)
  123.     {
  124.         OutputUsage();
  125.         exit(0);
  126.     }
  127.     
  128.     LibArgs.Flags = ((LibArgs.LIBNAMES && 0xFFFFFFFF) << LAFB_LIBNAMES) |
  129.                 ((LibArgs.ALL & 1) << LAFB_ALL) |
  130.                     ((LibArgs.LIST & 1) << LAFB_LIST) |
  131.                     ((LibArgs.FLUSH & 1) << LAFB_FLUSH);
  132.                
  133.     switch (LibArgs.Flags)
  134.     {
  135.     
  136.     /*-- No library names, ALL flag set --*/
  137.     case (LAFF_ALL):
  138.         LoadLibs();
  139.         break;
  140.     
  141.     /*-- No library names, LIST flag, or ALL and LIST flags set --*/
  142.     case (LAFF_LIST):
  143.     case (LAFF_LIST | LAFF_ALL):
  144.         ListLibs();
  145.         break;
  146.     
  147.     /*-- No library names, FLUSH flag, or ALL and FLUSH flags set --*/
  148.     case (LAFF_FLUSH):
  149.     case (LAFF_FLUSH | LAFF_ALL):
  150.         FlushLibs();
  151.         break;
  152.     
  153.     /*-- Library names, no flags set --*/
  154.     case (LAFF_LIBNAMES):
  155.         for (i=0; LibArgs.LIBNAMES[i] != NULL; i++)
  156.             LoadLib(LibArgs.LIBNAMES[i],REPORT);
  157.         break;
  158.     
  159.     /*-- Library names, LIST flag set --*/
  160.     case (LAFF_LIBNAMES | LAFF_LIST):
  161.         OutputLibInfoHeader();
  162.         for (i=0; LibArgs.LIBNAMES[i] != NULL; i++)
  163.             ListLib(LibArgs.LIBNAMES[i]);
  164.         break;
  165.     
  166.     /*-- Library names, FLUSH flag set --*/
  167.     case (LAFF_LIBNAMES | LAFF_FLUSH):
  168.         for(i=0; LibArgs.LIBNAMES[i] != NULL; i++)
  169.             FlushLib(LibArgs.LIBNAMES[i]);
  170.         break;
  171.     
  172.     /*-- Default --*/
  173.     default:
  174.         OutputUsage();
  175.     }
  176.  
  177.     FreeArgs(rdargs);
  178. } /* End */
  179.  
  180. /*--- Processing ---*/
  181.  
  182. /* Load specified library */
  183. void LoadLib(STRPTR LibName, BOOL Report)
  184. {
  185.     struct Library *Library;
  186.  
  187.     Library = OldOpenLibrary(LibName);
  188.     if (Library)
  189.     {
  190.         printf("%s v%d.%d loaded.\n",LibName,
  191.                                      Library->lib_Version,
  192.                                      Library->lib_Revision);
  193.         CloseLibrary(Library);
  194.     }
  195.     else if (Report)
  196.         printf("Couldn't open %s.\n",LibName);
  197. }
  198.  
  199. /* Load all libraries in the LIBS: assignment */
  200. void LoadLibs(void)
  201. {
  202.     BPTR LibsLock;
  203.     struct FileInfoBlock LibsFIB;
  204.  
  205.     LibsLock = Lock("LIBS:",SHARED_LOCK);
  206.     if (!LibsLock)
  207.         puts("LIBS: is not available!");
  208.     else
  209.     {
  210.         Examine(LibsLock,&LibsFIB);
  211.  
  212.         while (ExNext(LibsLock,&LibsFIB))
  213.         {
  214.             LoadLib(LibsFIB.fib_FileName,QUIET);
  215.             if (CTRL_C)
  216.             {
  217.                 puts(BREAK_TXT);
  218.                 break;
  219.             }
  220.         }
  221.         
  222.         UnLock(LibsLock);
  223.     }
  224. }
  225.  
  226. /* Flush specified library */
  227. void FlushLib(STRPTR LibName)
  228. {
  229.     struct Library *Library;
  230.     struct LibInfo *LibInfo = NULL;
  231.  
  232.     Forbid();
  233.     Library = (struct Library *)FindName(&SysBase->LibList,LibName);
  234.     if (Library)
  235.     {
  236.         LibInfo = AddLibInfo(NULL,Library);
  237.         if ((BOOL)RemLibrary((struct Library *)LibInfo->Address))
  238.             LibInfo->Flushed = TRUE;
  239.     }
  240.     Permit();
  241.     
  242.     if (LibInfo)
  243.     {
  244.         if (LibInfo->Flushed == TRUE)
  245.             OutputFlushedLibInfo(LibInfo);
  246.         KillLibInfo(LibInfo);
  247.     }
  248.     else
  249.         OutputLibNotFound(LibName);
  250. }
  251.  
  252. /* Flush all libraries from memory */
  253. void FlushLibs(void)
  254. {
  255.     struct LibInfo *LibInfo;
  256.     struct LibInfo *LibInfoHead;
  257.  
  258.     Forbid();
  259.     LibInfo = BuildLibInfoList(&SysBase->LibList);
  260.     LibInfoHead = LibInfo;
  261.  
  262.     while (LibInfo)
  263.     {
  264.         if ((BOOL)RemLibrary((struct Library *)LibInfo->Address))
  265.             LibInfo->Flushed = TRUE;
  266.         LibInfo = LibInfo->Next;
  267.     }
  268.     Permit();
  269.  
  270.     LibInfo = LibInfoHead;
  271.     while (LibInfo)
  272.     {
  273.         if (LibInfo->Flushed == TRUE)
  274.             OutputFlushedLibInfo(LibInfo);
  275.         LibInfo = LibInfo->Next;
  276.     }
  277.     
  278.     KillLibInfo(LibInfoHead);
  279. }
  280.  
  281. /* List specified library */
  282. void ListLib(STRPTR LibName)
  283. {
  284.     struct Library *Library;
  285.     struct LibInfo *LibInfo = NULL;
  286.  
  287.     Forbid();
  288.     Library = (struct Library *)FindName(&SysBase->LibList,LibName);
  289.     if (Library)
  290.         LibInfo = AddLibInfo(NULL,Library);
  291.     Permit();
  292.  
  293.     if (LibInfo)
  294.     {
  295.         OutputLibInfo(LibInfo);
  296.         KillLibInfo(LibInfo);
  297.     } 
  298.     else
  299.         OutputLibNotFound(LibName);
  300. }
  301.  
  302. /* List all libraries in memory */
  303. void ListLibs(void)
  304. {
  305.     struct LibInfo *LibInfo;
  306.     struct LibInfo *LibInfoHead;
  307.  
  308.     Forbid();
  309.     LibInfo = BuildLibInfoList(&SysBase->LibList);
  310.     LibInfoHead = LibInfo;
  311.     Permit();
  312.  
  313.     OutputLibInfoHeader();
  314.     while (LibInfo)
  315.     {
  316.         OutputLibInfo(LibInfo);
  317.         LibInfo = LibInfo->Next;
  318.         if (CTRL_C)
  319.         {
  320.             puts(BREAK_TXT);
  321.             break;
  322.         }
  323.     }
  324.  
  325.     KillLibInfo(LibInfoHead);
  326. }
  327.  
  328. /* Build and return a Library Info list of all libraries in memory */
  329. struct LibInfo *BuildLibInfoList(struct List *LibList)
  330. {
  331.     struct Library *Library;
  332.     struct LibInfo *LibInfo = NULL;
  333.  
  334.     Library = (struct Library *)LibList->lh_Head;
  335.     while (Library->lib_Node.ln_Succ)
  336.     {
  337.         LibInfo = AddLibInfo(LibInfo,Library);
  338.         Library = (struct Library *)Library->lib_Node.ln_Succ;
  339.     }
  340.  
  341.     LibInfo = GetLibInfoHead(LibInfo);
  342.  
  343.     return LibInfo;
  344. }
  345.  
  346. /*--- LibInfo List Managment ---*/
  347.  
  348. /* Add specified library data to specified Library Info list */
  349. struct LibInfo *AddLibInfo(struct LibInfo *LibInfo, struct Library *Library)
  350. {
  351.     struct LibInfo *NewLibInfo;
  352.  
  353.     NewLibInfo = (struct LibInfo *)calloc(sizeof(*NewLibInfo),1);
  354.  
  355.     strcpy(NewLibInfo->Name,Library->lib_Node.ln_Name);
  356.     NewLibInfo->Address = (ULONG)Library;
  357.     NewLibInfo->Version = Library->lib_Version;
  358.     NewLibInfo->Revision = Library->lib_Revision;
  359.     NewLibInfo->Priority = Library->lib_Node.ln_Pri;
  360.     NewLibInfo->OpenCount = Library->lib_OpenCnt;
  361.     NewLibInfo->Flushed = FALSE;
  362.  
  363.     if (LibInfo)
  364.     {
  365.         LibInfo = GetLibInfoTail(LibInfo);
  366.         NewLibInfo->Prev = LibInfo;
  367.         LibInfo->Next = NewLibInfo;
  368.     }
  369.  
  370.     return NewLibInfo;
  371. }
  372.  
  373. /* Destroy specified Library Information list */
  374. void KillLibInfo(struct LibInfo *LibInfo)
  375. {
  376.     struct LibInfo *NextLibInfo;
  377.  
  378.     while (LibInfo)
  379.     {
  380.         NextLibInfo = LibInfo->Next;
  381.         free(LibInfo);
  382.         LibInfo = NextLibInfo;
  383.     }
  384. }
  385.  
  386. /* Get head of specified Library Info list */
  387. struct LibInfo *GetLibInfoHead(struct LibInfo *LibInfo)
  388. {
  389.     while (LibInfo->Prev)
  390.         LibInfo = LibInfo->Prev;
  391.  
  392.     return LibInfo;
  393. }
  394.  
  395. /* Get tail of specified Library Info list */
  396. struct LibInfo *GetLibInfoTail(struct LibInfo *LibInfo)
  397. {
  398.     while (LibInfo->Next)
  399.         LibInfo = LibInfo->Next;
  400.  
  401.     return LibInfo;
  402. }
  403.  
  404. /*--- Output ---*/
  405.  
  406. /* Output LibMon header */
  407. void OutputHeader(void)
  408. {
  409.     BoldText();
  410.     printf("Library Monitor v%s © %s Karl J. Ots\n",PROG_VERS,
  411.                                                       PROG_DATE);
  412.     PlainText();
  413.     puts("");
  414. }
  415.  
  416. /* Output usage information */
  417. void OutputUsage(void)
  418. {
  419.     puts("Usage: LibMon <libraries> <ALL> <LIST> <FLUSH>");
  420.     puts("");
  421. }
  422.  
  423. /* Output Library Info Header */
  424. void OutputLibInfoHeader(void)
  425. {
  426.     Colour2Text();
  427.     puts("Address    Library                     Version     Pri   OpenCnt");
  428.     PlainText();
  429. }
  430.  
  431. /* Output specified Library Info data */
  432. void OutputLibInfo(struct LibInfo *li)
  433. {
  434.     printf("%08X   %-25s   %3d.%-4d   %4d   %5d\n",li->Address,
  435.                                                    li->Name,
  436.                                                    li->Version,
  437.                                                    li->Revision,
  438.                                                    li->Priority,
  439.                                                    li->OpenCount);
  440. }
  441.  
  442. /* Output flushed Library Information data */
  443. void OutputFlushedLibInfo(struct LibInfo *li)
  444. {
  445.     printf("%s v%d.%d flushed.\n",li->Name,
  446.                                   li->Version,
  447.                                   li->Revision);
  448. }
  449.  
  450. /* Output that library could not be found */
  451. void OutputLibNotFound(STRPTR name)
  452. {
  453.     printf("           %s was not found in memory.\n",name);
  454. }
  455.  
  456. /*--- Text colour/style ---*/
  457.  
  458. /* Change to plain text */
  459. void PlainText(void)
  460. {
  461.     printf("\033[0m");
  462. }
  463.  
  464. /* Change to bold text */
  465. void BoldText(void)
  466. {
  467.     printf("\033[1m");
  468. }
  469.  
  470. /* Change text colour to #2 */
  471. void Colour2Text(void)
  472. {
  473.     printf("\033[32m");
  474. }
  475.  
  476. /* Change text colour to #3 */
  477. void Colour3Text(void)
  478. {
  479.     printf("\033[33m");
  480. }
  481.  
  482. /*--- Miscellaneous ---*/
  483.  
  484. /* Disable automatic program abortion if present */
  485. void chkabort(void)
  486. {
  487.     ;
  488. }
  489.